//
//  CONodataView.swift
//  CocoProject
//
//  Created by Vill on 2023/11/7.
//

import UIKit

class CONodataView: UIView {

    var emptyImageView:UIImageView?
    var emptyLabel:UILabel?
    
    static func emptyView() -> CONodataView {
        let emptyView = CONodataView.init()
        emptyView.backgroundColor = UIColor.clear
        return emptyView
    }

    init() {
        super.init(frame: .zero)
        
        emptyImageView = UIImageView.init()
        emptyImageView?.image = UIImage(named: "coco_nodata ")
        self.addSubview(emptyImageView!)
        emptyImageView?.snp.makeConstraints({ make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-80)
        })
        
        emptyLabel = UILabel.init()
        emptyLabel?.text = "no data"
        emptyLabel?.font = UIFont.pingfangFont(ofSize: PTW(15), weight: .semibold)
        emptyLabel?.textColor = UIColor.white.withAlphaComponent(0.4)
        self.addSubview(emptyLabel!)
        emptyLabel?.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(emptyImageView!.snp.bottom).offset(15)
        }
    }
    
    func dg_show(_ icon:String, _ message:String, _ view:UIView) {
        self.frame = view.bounds
        if message.count > 0 {
            self.emptyLabel?.text = message
        }
        if icon.count > 0   {
            self.emptyImageView?.image = UIImage(named: icon)
        }
        view.addSubview(self)
    }

    static func dg_show(_ view:UIView) {
        CONodataView.dg_hide(view)
        let emptyView = CONodataView.emptyView()
        emptyView.dg_show("", "", view)
    }
    
    static func dg_show(_ icon:String, _ message:String, _ view:UIView) {
        
        CONodataView.dg_hide(view)
        let emptyView = CONodataView.emptyView()
        emptyView.dg_show(icon, message, view)
    }
    
    static func dg_hide(_ view:UIView) {
        
        for item in view.subviews {
            if let item = item as? CONodataView{
                item.removeFromSuperview()
            }
        }
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
